package com.jing.cloud.service.group.method;

import com.jing.cloud.service.group.bean.Group;
import com.jing.cloud.service.group.constant.GroupConstanct;
import com.jing.cloud.service.group.helper.GroupHelper;
import com.jing.cloud.service.group.method.param.GroupCreateBean;
import com.jing.cloud.service.method.AbstractMethod;
import com.jing.cloud.service.method.Method;
import com.jing.cloud.util.ErrCode;
import com.jing.cloud.service.Res;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Set;

/**
 * Created by 29017 on 2017/9/19.
 */
public class NewGroup extends AbstractMethod<GroupCreateBean> implements Method<GroupCreateBean> {

    @Autowired
    GroupHelper groupHelper;

    @Override
    public Res call(GroupCreateBean groupCreateBean) throws Exception {
        Group group = new Group();
        long userId = getUserId();
        group.setGroupName(groupCreateBean.getGroupName());
        group.setCreateUserId(userId);
        group.setIcon(groupCreateBean.getIcon());
        Res res = groupHelper.createGroup(group);
        if (res.getCode() == ErrCode.SUCCESS) {
            Set<Long> uids = groupCreateBean.getMembers();
            uids.remove(userId);
            groupHelper.addMember(group.getId(), uids, GroupConstanct.GROUP_LEVEL_NORMAL);
        }
        return res;
    }
}
